package com.shigu.advert.service;

import com.opentae.data.mall.beans.MallAdvertising;
import com.opentae.data.mall.beans.MallItem;
import com.opentae.data.mall.beans.MallItemcats;
import com.opentae.data.mall.examples.MallAdvertisingExample;
import com.opentae.data.mall.interfaces.MallAdvertisingMapper;
import com.opentae.data.mall.interfaces.MallItemMapper;
import com.opentae.data.mall.interfaces.MallItemcatsMapper;
import com.shigu.PageResponse;
import com.shigu.advert.api.AdvertService;
import com.shigu.advert.bo.AdvertBo;
import com.shigu.advert.bo.ModifyAdvertBo;
import com.shigu.advert.vo.AdvertVo;
import com.shigu.advert.vo.IndexAdvert;
import com.shigu.exceptions.AdminException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/*
 * create by  过儿  2019-06-11
 *
 */
@Service(value = "advertServiceImpl")
public class AdvertServiceImpl implements AdvertService {

    @Autowired
    MallAdvertisingMapper mallAdvertisingMapper;
    @Autowired
    MallItemMapper mallItemMapper;
    @Autowired
    MallItemcatsMapper mallItemcatsMapper;

    private static String tuijian = "推荐";

    @Override
    public PageResponse<AdvertVo> advertList(int page, int size, String itemCode,String cid) {
        MallAdvertisingExample example = new MallAdvertisingExample();
        MallAdvertisingExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(itemCode)) {
            criteria.andItemCodeEqualTo(itemCode);
        }
        if(StringUtils.isNotBlank(cid)){
            criteria.andCidEqualTo(Long.valueOf(cid));
        }
        example.setOrderByClause("weight DESC");
        example.setStartIndex((page - 1) * size);
        example.setEndIndex(size);
        List<MallAdvertising> list = mallAdvertisingMapper.selectByConditionList(example);
        int total = mallAdvertisingMapper.countByExample(example);
        PageResponse<AdvertVo> response = new PageResponse<>();
        response.setTotal(total);
        response.setData(list.stream().map(o -> {
            MallItemcats cate = mallItemcatsMapper.selectByPrimaryKey(o.getCid());
            //商品信息根据商品id 查询
            MallItem item = mallItemMapper.selectByPrimaryKey(o.getItemId());
            AdvertVo vo = new AdvertVo();
            vo.setAdvertId(o.getAdvertId());
            vo.setTitle(item.getTitle());
            vo.setImgUrl(o.getImgUrl());
            vo.setItemCode(item.getItemCode());
            vo.setWeight(o.getWeight());
            vo.setRemark(o.getRemark());
            vo.setCid(o.getCid());
            vo.setCateName(cate==null?tuijian:cate.getCatName());
            return vo;
        }).collect(Collectors.toList()));

        return response.success();
    }

    @Override
    public void addAdvert(AdvertBo bo) throws AdminException {
        MallItem item = mallItemMapper.selectByPrimaryKey(bo.getItemId());
        if (item == null) {
            throw new AdminException("不存在此商品");
        }
        long cid = bo.getCid()==null?-1L:bo.getCid();
        MallAdvertisingExample example = new MallAdvertisingExample();
        example.createCriteria().andItemIdEqualTo(bo.getItemId()).andCidEqualTo(cid);
        List<MallAdvertising> list = mallAdvertisingMapper.selectByExample(example);
        if (list.size() > 0) {
            throw new AdminException("该类目下已有该商品");
        }

        MallAdvertising advert = new MallAdvertising();
        advert.setTitle(item.getTitle());
        advert.setItemCode(item.getItemCode());
        advert.setItemId(item.getItemId());
        advert.setImgUrl(bo.getImgUrl());
        advert.setWeight(bo.getWeight());
        advert.setCid(cid);
        mallAdvertisingMapper.insertSelective(advert);
    }

    @Override
    public void delAdvert(Long advertId) {
        mallAdvertisingMapper.deleteByPrimaryKey(advertId);
    }

    @Override
    public void modifyAdvert(ModifyAdvertBo bo) {
        MallAdvertising advert = new MallAdvertising();
        advert.setAdvertId(bo.getAdvertId());
        advert.setItemId(bo.getItemId());
        advert.setImgUrl(bo.getImgUrl());
        advert.setRemark(bo.getRemark());
        advert.setCid(bo.getCid());
        advert.setWeight(bo.getWeight());
        mallAdvertisingMapper.updateByPrimaryKeySelective(advert);
    }

    @Override
    public PageResponse<IndexAdvert> indexSwipe(String mainCateId) {
        //类目id ，为空时 为-1
        Long cid = StringUtils.isBlank(mainCateId)? -1L : Long.valueOf(mainCateId);
        MallAdvertisingExample example = new MallAdvertisingExample();
        example.createCriteria().andCidEqualTo(cid);
        example.setOrderByClause("weight desc");
        List<MallAdvertising> list = mallAdvertisingMapper.selectByExample(example);

        PageResponse<IndexAdvert> response = new PageResponse<>();
        response.setTotal(list.size());
        response.setData(list.stream().map(o->{
            IndexAdvert vo = new IndexAdvert();
            vo.setImgUrl(o.getImgUrl());
            vo.setItemId(o.getItemId());
            return vo;
        }).collect(Collectors.toList()));

        return response.success();
    }

}
